package cn.imnaer.database;

import com.sequoiadb.base.DBCollection;
import com.sequoiadb.base.DBCursor;
import com.sequoiadb.exception.BaseException;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.List;

public class SequoiaDataAccess {

	public BSONObject queryOne(DBCollection dbcollection, BSONObject matcher,
			BSONObject selector, BSONObject order) {
		return dbcollection.queryOne(matcher, selector, order, null, 0);
	}

	public List<BSONObject> queryAll(DBCollection dbcollection,
			BSONObject matcher, BSONObject selector, BSONObject order) {
		return this.queryAll(dbcollection, matcher, selector, order, 0, -1);
	}
	public List<BSONObject> queryAll(DBCollection dbcollection) {
		List<BSONObject> list = new ArrayList<BSONObject>();
		DBCursor cursor = null;
		try {
			cursor = dbcollection.query();
			while (cursor.hasNext()) {
				BSONObject record = cursor.getNext();
				list.add(record);
			}
		} catch (BaseException e) {

			e.printStackTrace();
		}
		return list;
	}


	public List<BSONObject> queryAll(DBCollection dbcollection,
			BSONObject matcher, BSONObject selector, BSONObject order,
			long skipRows, long returnRows) {
		List<BSONObject> list = new ArrayList<BSONObject>();
		DBCursor cursor = null;
		try {
			cursor = dbcollection.query(matcher, selector, order, null,
					skipRows, returnRows);
			while (cursor.hasNext()) {
				BSONObject record = cursor.getNext();
				list.add(record);
			}
		} catch (BaseException e) {

			e.printStackTrace();
		}
		return list;
	}

	public List<BSONObject> aggregate(DBCollection dbcollection,
			BSONObject match, BSONObject group, BSONObject project) {
		List<BSONObject> records = new ArrayList<BSONObject>();
		DBCursor cursor = null;
		try {

			List<BSONObject> obj = new ArrayList<BSONObject>();
			BSONObject matcher = new BasicBSONObject();
			matcher.put("$match", match);
			obj.add(match);
			// grouper.put("_id", arg1)
			obj.add(group);
			obj.add(project);
			dbcollection.aggregate(obj);
			while (cursor.hasNext()) {
				BSONObject record = cursor.getNext();
				records.add(record);
			}
		} catch (BaseException e) {

			e.printStackTrace();
		}
		return records;
	}

	public void update(DBCollection dbcollection, BSONObject matcher, Object obj) {
		BSONObject modifier = new BasicBSONObject();
		
		try {
			modifier.put("$set",BasicBSONObject.typeToBson(obj));
			dbcollection.update(matcher, modifier, null);
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public void userDefinedUpdate(DBCollection dbcollection,
			BSONObject matcher, Object record) {
		try {
			BSONObject modifier = BasicBSONObject.typeToBson(record);
			dbcollection.update(matcher, modifier, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updateCounter(DBCollection dbcollection, BSONObject matcher,
			String field, int increment) {
		try {
			BSONObject obj = new BasicBSONObject();
			// "$inc":{"usefulCount" : -1}}
			BSONObject rule = new BasicBSONObject();
			rule.put(field, increment);
			obj.put(MatchConst._INC, rule);
			dbcollection.update(matcher, obj, null);
		} catch (BaseException e) {
			e.printStackTrace();
		}
	}

	public void delete(DBCollection dbcollection, BSONObject matcher) {
		try {
			dbcollection.delete(matcher);
		} catch (BaseException e) {

			e.printStackTrace();
		}
	}

	public void insert(DBCollection dbcollection, Object obj) {
		if (obj != null) {
			try {
				BSONObject bson = BasicBSONObject.typeToBson(obj);
				dbcollection.insert(bson);
			} catch (Exception e) {

				e.printStackTrace();
			}
		}
	}

	public void batchInsert(DBCollection dbcollection, List<BSONObject> items) {
		dbcollection.bulkInsert(items, 0);
	}
}
